# -*- encoding:utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
import requests
import json

class PS(object):
    def __init__(self):
        self.url=settings.PS_DOMAIN+'/cmd/'
    
    def request(self,dc):
        try:
            rt = requests.post(self.url,json.dumps(dc))
        except Exception as e:
            raise UserWarning,'connect to PS has error, may be PS not online' 
        
        if rt.status_code!=200:
            print(rt.text)
            raise UserWarning,'PS return code :{code} '.format(code=rt.status_code)  
        
        rt = json.loads(rt.text)
        if rt.get('msg'):
            raise UserWarning,rt.get('msg')
        else:
            return rt  
    
    def get_app_meta(self,sku):
        """
        rt:
             long_dsp
             short_dsp
             pub_name
             last_meta= True or False
        """
        dc ={
            'get_app_meta':{'sku':sku}
        }
        rt = self.request(dc)
        return rt.get('get_app_meta')
    
    
    def get_apps_meta(self,apps):
        """
        @ apps: list: app的sku列表，例如 [app_sku1,app_sku_2 ...]
        
        rt:
        [{long_dsp:xxx,short_dsp:xxx}]
        """
        dc={
            'get_apps_meta':{'apps':apps}
        }
        rt = self.request(dc)
        return rt.get('get_apps_meta')